package pl.piotrgrygiel.unittests;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class CheckLogin extends SeleniumBase {

	@Test
	public void testCheckLogin() throws Exception {
		//open
		driver.get("http://demo.bananascrum.com/login");
		
		// przypadek 1
		System.out.println("Sprawdzaj poprawny login bledny pass");
		insertText(By.id("login"),"admin");
		insertText(By.id("password"),"jab");
		click(By.name("commit"));
		click(By.id("flash"));
		
		assertText("Login failed Type user 'admin' with password 'password'", By.id("flash"));
		System.out.println("sprawdzaj -> OK");
		
		// przypadek 2
		System.out.println("Sprawdzaj bledny login dobry pass");
		insertText(By.id("login"),"jajo");
		insertText(By.id("password"),"password");
		click(By.name("commit"));
		click(By.id("flash"));
		
		assertText("Login failed Type user 'admin' with password 'password'", By.id("flash"));
		System.out.println("sprawdzaj -> OK");
		
		// przypadek 3
		System.out.println("Sprawdzaj bledny login bledny pass");
		insertText(By.id("login"),"jajo");
		insertText(By.id("password"),"pasio");
		click(By.name("commit"));
		click(By.id("flash"));
		
		assertText("Login failed Type user 'admin' with password 'password'", By.id("flash"));
		System.out.println("sprawdzaj -> OK");
		
		// przypadek 4
		System.out.println("Sprawdzaj czy sie loguje");
		insertText(By.id("login"),"admin");
		insertText(By.id("password"),"password");
		click(By.name("commit"));
			
		WebElement messageContainer = driver.findElement(By.id("flash"));
	    assertTrue(messageContainer.getText().contains("Logged in successfully"));
	    assertTrue(messageContainer.isDisplayed());
	    System.out.println("Logowanie ok");
	    
		// sprawdz czy "logged in successfully" zanika
	    // czekam az zniknie
		
	    WebDriverWait wait = new WebDriverWait(driver, 10);
	    wait.until(new ExpectedCondition<Boolean>() {
	    	
	    	@Override
	    	public Boolean apply(WebDriver d){
	    		WebElement element = d.findElement(By.id("flash"));
	    		
	    		if(element.isDisplayed()) {
	    			return null;
	    		}
	    		
	    		return Boolean.TRUE;
	    	}
		});
	    		
	}

}
